<template>
    <div>
        <div>
            <el-col :span="3">
                <div class="grid-content bg-purple"></div>
                <el-button type="primary" @click="onhandAdd">添加厨师</el-button>
            </el-col>
            <el-input style="width: 240px;" placeholder="请输入姓名" v-model="params.chefAdminName"></el-input>
            <el-input style="width: 240px;margin-left: 10px;" placeholder="请输入工号" v-model="params.chefAdminNum">
            </el-input>
            <el-button style="margin-left: 10px;" type="primary" icon="el-icon-search " @click="loads">搜索</el-button>
            <el-button style="margin-left: 10px;" type="warning" icon="el-icon-scissors " @click="reset">重置</el-button>
        </div>
        <br>
        <div>
            <el-col :span="6">
                <div class="grid-content bg-purple"></div>
                <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                               title="确认删除所有选中的员工吗？" @confirm="deleteSelected">
                    <el-button type="danger" slot="reference" v-bind:disabled="disable">删除所选</el-button>
                </el-popconfirm>
            </el-col>
        </div>
        <el-table ref="multipleTable" :data="tableData" stripe style="width: 100%" tooltip-effect="dark"
                  @selection-change="handleSelectionChange">
            <el-table-column
                    prop="chefAdminId"
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="chefAdminNum" label="工号" width="180">
            </el-table-column>
            <el-table-column prop="chefAdminName" label="姓名" width="180">
            </el-table-column>
            <el-table-column prop="chefAdminSex" label="性别" width="180">
            </el-table-column>
            <el-table-column prop="chefAdminTel" label="电话号码" width="180">
            </el-table-column>
            <el-table-column prop="chefAdminAddress" label="住址" width="180">
            </el-table-column>
            <el-table-column label="操作" prop="chefAdminId">
                <template v-slot="scope">
                    <el-button type="primary" @click="handleEdit(scope.row.chefAdminId)">编辑
                    </el-button>
                    <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info"
                                   icon-color="yellow"
                                   title="确认重置该员工密码吗？" @confirm="resetPassword(scope.row.chefAdminId)">
                        <el-button type="warning" slot="reference">重置密码</el-button>
                    </el-popconfirm>
                    <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                                   title="确认删除此员工吗？" @confirm="del(scope.row.chefAdminId)">
                        <el-button type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin-top: 20px">
            <el-button @click="toggleSelection()">取消选择</el-button>
        </div>
        <!-- 分页 -->
        <div style="margin-top: 10px;">
            <el-pagination background :current-page="params.pageNum" :page-size="params.pageSize"
                           layout="prev, pager, next" :total="total" @current-change="handleCurrentChange">
            </el-pagination>
        </div>
        <!-- 新增编辑弹框 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-form :inline="true" :model="chefadminform" status-icon :rules="rules" ref="chefadminform"
                     style="width: 80%;"
                     label-width="200px">
                <el-form-item label="姓名" prop="chefAdminName">
                    <el-input v-model="chefadminform.chefAdminName" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="chefAdminSex">
                    <el-switch
                            v-model="chefadminform.chefAdminSex"
                            active-text="男"
                            active-color="#409EFF"
                            inactive-text="女"
                            inactive-color="pink">

                    </el-switch>
                </el-form-item>
                <el-form-item label="电话号码" prop="chefAdminTel">
                    <el-input v-model="chefadminform.chefAdminTel" placeholder="请输入电话号码"></el-input>
                </el-form-item>
                <el-form-item label="住址" prop="chefAdminAddress">
                    <el-input v-model="chefadminform.chefAdminAddress" placeholder="请输入住址"></el-input>
                </el-form-item>

            </el-form>
            <div style="text-align-last: center;width: 60%;">
                <el-button @click="closedialog('chefadminform')" type="danger">取 消</el-button>
                <el-button @click="resetForm('chefadminform')" type="warning">重置</el-button>
                <el-button style="margin-right: 30px;" type="primary" icon="el-icon-upload2 "
                           @click="dialogVisibleConfirm('chefadminform')" size="medium">{{save}}
                </el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import request from '@/request'

    export default {
        name: 'ManagechefAdmin',
        data() {
            const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
            var checkTel = (rule, value, callback) => {
                console.log(rule)
                console.log(value)
                if (!value) {
                    return callback(new Error('请输入电话号码'));
                }
                setTimeout(() => {
                    if (!regMobile.test(value)) {
                        callback(new Error('请输入正确的电话号码'));
                    } else {
                        callback();
                    }
                }, 1000);
            };
            return {
                //将所有选中的id存全局
                multipleSelection: [],
                //后端返回的数据
                tableData: [],
                //后端返回的页数
                total: 0,
                //id存全局，判断以新增还是修改出现dialog
                allid: "",
                //dialog的标题
                title: "",
                //dialog的按钮
                save: '',
                //判断删除所选是否可用
                disable: true,
                //判断dialog是否显示
                dialogVisible: false,
                //分页模糊查询的请求体
                params: {
                    pageNum: 1,
                    pageSize: 8,
                },
                //dialog的表单
                chefadminform: {
                    chefAdminName: '',
                    chefAdminSex: true,
                    chefAdminTel: '',
                    chefAdminAddress: ''
                },
                //dialog的校验规则
                rules: {
                    chefAdminName: [{
                        required: true,
                        message: '衣物种类名称不能为空',
                        trigger: 'blur'
                    },],
                    chefAdminTel: [{
                        validator: checkTel,
                        trigger: 'blur',
                        required: true,
                    },],

                }
            }
        },
        created() {
            this.load()
        },
        methods: {
            //取消所有多选
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            //判断删除所选按钮是否可用以及将已选id存全局
            handleSelectionChange(val) {
                if (val.length == 0) {
                    this.multipleSelection.length = val.length
                    this.disable = true
                } else {
                    this.disable = false
                    for (let i = 0; i < val.length; i++) {
                        this.multipleSelection[i] = val[i].chefAdminId
                    }
                }
            },
            //新增方法
            add(formName) {
                if (this.chefadminform.chefAdminSex) {
                    this.chefadminform.chefAdminSex = 1;
                } else {
                    this.chefadminform.chefAdminSex = 0;
                }
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        request.post('/chefadmin/add', this.chefadminform).then(res => {
                            if (res.code === '200') {
                                this.$notify.success('新增成功')
                                this.resetForm(formName) //清空表格数据
                                this.dialogVisible = false; //关闭弹框
                                this.load(); //刷新列表
                            } else {
                                this.$notify.error(res.msg)
                            }
                        })
                    }
                });
            },
            //修改方法
            update(formName) {
                if (this.chefadminform.chefAdminSex) {
                    this.chefadminform.chefAdminSex = 1;
                } else {
                    this.chefadminform.chefAdminSex = 0;
                }
                request.put('/chefadmin/update', this.chefadminform).then(res => {
                    console.log(res)
                    if (res.code === '200') {
                        this.$notify.success('修改成功')
                        this.resetForm(formName) //清空表格数据
                        this.dialogVisible = false; //关闭弹框
                        this.load(); //刷新列表
                    } else {
                        this.$notify.error(res.msg)
                    }
                })
            },
            //删除方法
            del(chefAdminId) {
                request.delete('/chefadmin/delete' + chefAdminId).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('删除成功')
                        this.load();
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            },
            //重置密码方法
            resetPassword(chefAdminId) {
                request.put('/chefadmin/resetpassword' + chefAdminId).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('重置成功，初始密码为123')
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            },
            //批量删除方法
            deleteSelected() {
                console.log(this.multipleSelection)
                request.post('/chefadmin/deletes', this.multipleSelection).then(res => {
                    console.log(res)
                    if (res.code === '200') {
                        this.$notify.success('批量删除成功')
                        this.load(); //刷新列表
                    } else {
                        this.$notify.error(res.msg)
                    }
                })
            },
            //关闭dialog
            handleClose(done) {
                this.dialogVisible = false;
            },
            //添加按钮，从此处调取添加方法
            onhandAdd() {
                this.title = '添加员工';
                this.save = "添加"
                this.resetForm('chefadminform');
                this.dialogVisible = true;
            },
            //编辑按钮，从此处调取修改方法
            handleEdit(chefAdminId) {
                this.save = "保存"
                this.title = '修改员工信息'
                this.dialogVisible = true; //显示弹框
                request.get("/chefadmin/" + chefAdminId).then(res => {
                    this.chefadminform = res.data
                    if (this.chefadminform.chefAdminSex == 1) {
                        this.chefadminform.chefAdminSex = true
                    } else this.chefadminform.chefAdminSex = false
                })//row当前行数据，把当前行的数据赋值给 表单
                this.allid = chefAdminId; //把id存全局
            },
            //清空表单
            resetForm(formName) {
                this.$nextTick(() => {
                    this.$refs[formName].resetFields();
                })
            },
            //页面加载
            loads() {
                this.params.pageNum = 1
                this.load()
            },
            //初始化页面
            load() {
                request.get('/chefadmin/page', {
                    params: this.params
                }).then(res => {
                    if (res.code === "200") {
                        this.total = res.data.total;
                        this.tableData = res.data.list;
                        for (let i = 0; i < this.tableData.length; i++) {
                            if (this.tableData[i].chefAdminSex == 1) {
                                this.tableData[i].chefAdminSex = "男"
                            } else this.tableData[i].chefAdminSex = "女"
                        }
                    }
                })

            },
            //重置方法
            reset() {
                this.params = {
                    user_name: '',
                    user_num: '',
                    pageNum: 1,
                    pageSize: 10
                }
                this.load()
            },
            //核心判断哪一个按钮调用表单组件
            dialogVisibleConfirm(formName) {
                // 新增接口
                if (!this.allid) {
                    this.add(formName)

                } else {
                    // 修改接口
                    this.update(formName);
                }
            },
            //关闭dialog
            closedialog(formName) {
                this.dialogVisible = false;
                this.resetForm(formName) //清空表格数据
            },
            //切换页码
            handleCurrentChange(pageNum) {
                this.params.pageNum = pageNum;
                this.load()
            }
        }
    }
</script>
